package com.gitee.huxing.framework.redis;

import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Description: redis工具装配
 * @ProjectName: JavaCourse
 * @Package: com.gitee.huxing.framework.redis
 * @ClassName: RedisWrapper
 * @Author: huxing
 * @DateTime: 2021-10-13 下午6:23
 */
@SuppressWarnings("all")
public class RedisWrapper {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RedisUtils redisUtil;

    @Bean
    public RedisUtils createRedisUtils(RedisTemplate redisTemplate){
        return new RedisUtils(redisTemplate);
    }

    /**
     * @Description: 缓存数据
     * @Author: huxing
     * @param key   key
     * @param value value
     * @Date: 2021/10/13 下午6:27
     **/
    public void setObject(String key, Object value){
        this.setObject(key, value, null);
    }

    /**
     * @Description:  缓存数据(带缓存时间)
     * @Author: huxing
     * @param key    key
     * @param value  value
     * @param time   失效时间
     * @Date: 2021/10/13 下午6:27
     **/
    public void setObject(String key, Object value, Long time){
        if(StringUtils.isEmpty(key) || value==null){
            return;
        }
        //判断当前是否是String类型的
        if(value instanceof String){
            //存放String类型
            String stringValue=(String) value;
            if(time==null){
                redisTemplate.opsForValue().set(key,stringValue);
            }else{
                redisTemplate.opsForValue().set(key,stringValue,time, TimeUnit.SECONDS);
            }
        } else if(value instanceof List){
            //存放List类型
            List<String> list=(List<String>) value;
            for (String str:list) {
                redisTemplate.opsForList().leftPush(key, str);
            }
        } else{
            if(time==null){
                redisTemplate.opsForValue().set(key, value);
            }else{
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            }
        }
    }

    /**
     * @Description: 获取缓存内容
     * @Author: huxing
     * @param key
     * @return java.lang.String
     * @Date: 2021/10/13 下午6:28
     **/
    public String getString(String key){
        return (String)redisUtil.get(key);
    }

    /**
     * @Description: 获取缓存内容
     * @Author: huxing
     * @param key
     * @return java.lang.String
     * @Date: 2021/10/13 下午6:28
     **/
    public Integer getInt(String key){
        return (Integer) redisUtil.get(key);
    }

    /**
     * @Description: 删除数据内容
     * @Author: huxing
     * @param key
     * @Date: 2021/10/13 下午6:28
     **/
    public void delKey(String key){
        stringRedisTemplate.delete(key);
    }

    /**
     * @Description: 获取分布式锁
     * @Author: huxing
     * @param lockId
     * @param millisecond
     * @return boolean
     * @Date: 2021/10/19 下午6:32
     **/
    public boolean getLock(String lockId, long millisecond) {
        Boolean success = redisTemplate.opsForValue().setIfAbsent(lockId, "lock",
                millisecond, TimeUnit.MILLISECONDS);
        return success != null && success;
    }

    /**
     * @Description: 释放锁
     * @Author: huxing
     * @param lockId
     * @Date: 2021/10/19 下午6:32
     **/
    public void releaseLock(String lockId) {
        redisTemplate.delete(lockId);
    }

    /**
     * @Description: 消息订阅发布
     * @Author: huxing
     * @param channel  渠道编号
     * @param obj      消息内容
     * @Date: 2021/10/26 下午2:11
     **/
    public <T> void publish(String channel, T obj){
        redisTemplate.convertAndSend(channel, obj);
    }

    /**
     * @Description: 默认递增计数
     * @Author: huxing
     * @param key
     * @return long
     * @Date: 2021/10/26 下午2:22
     **/
    public long incr(String key){
        return redisUtil.incr(key, 1);
    }

    /**
     * @Description: 递增计数
     * @Author: huxing
     * @param key
     * @param delta
     * @return long
     * @Date: 2021/10/26 下午2:22
     **/
    public long incr(String key, long delta){
        return redisUtil.incr(key, delta);
    }

    /**
     * @Description: 默认递减计数
     * @Author: huxing
     * @param key
     * @return long
     * @Date: 2021/10/26 下午2:22
     **/
    public long decr(String key){
        return redisUtil.decr(key, 1);
    }

    /**
     * @Description: 递减计数
     * @Author: huxing
     * @param key
     * @param delta
     * @return long
     * @Date: 2021/10/26 下午2:22
     **/
    public long decr(String key, long delta){
        return redisUtil.decr(key, delta);
    }
}
